package com.jurassic.demo.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @Author pan_jin
 * @Description: $
 * @Date: 2022/7/14 9:48
 * @Version: 1.0.0
 * @Slogan: 梦想还是要有的万一实现了呢
 */
@Component
@Aspect
public class LoggingAspect {

    @Before("execution(public * com.jurassic.demo.service.UserService.*(..))")
    public void doAccessCheck() {
        System.out.println("before do access check");
    }


    @Around("execution(public * com.jurassic.demo.service.MailService.*(..))")
    public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around start ...");
        Object object = pjp.proceed();
        System.out.println("around end ...");
        return object;
    }

    @Around("@annotation(metricTime)")
    public void doMetricTime(ProceedingJoinPoint pjp, MetricTime metricTime) throws Throwable {
        String name = metricTime.value();
        long start = System.currentTimeMillis();
        pjp.proceed();
        long end = System.currentTimeMillis();
        System.out.println(name + ":" + (end - start));

    }
}
